home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / password / source / installpassword.c < prev    next >
C/C++ Source or Header  |  1993-04-26  |  10KB  |  384 lines

  1. #include <exec/memory.h>
  2. #include <exec/execbase.h>
  3. #include <dos/filehandler.h>
  4. #include <devices/hardblocks.h>
  5. #include <devices/scsidisk.h>
  6. #include <devices/trackdisk.h>
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10.  
  11. #include <MyStartup.h>
  12. #include <MyLib.h>
  13.  
  14. /***********************************************/
  15.  
  16. #define NoBlock 0xffffffff
  17.  
  18. #define BLOCKS_NEEDED 4
  19.  
  20. /***********************************************/
  21.  
  22. extern void DriveInitCode(void);
  23. extern void CodeEnd(void);
  24.  
  25. /***********************************************/
  26.  
  27. struct RDArgs *RDArgs;
  28.  
  29. struct RigidDiskBlock RigidDiskBlock;
  30. struct BadBlockBlock BadBlockBlock;
  31. struct PartitionBlock PartitionBlock;
  32. struct FileSysHeaderBlock FileSysHeaderBlock;
  33. struct LoadSegBlock LoadSegBlock;
  34.  
  35. UBYTE *HardBlockMap;
  36. ULONG HardBlocksHi;
  37.  
  38. ULONG Blocks[BLOCKS_NEEDED];
  39.  
  40. ULONG RDBBlock;
  41.  
  42. struct IOStdReq IORequest;
  43.  
  44. struct
  45.    {
  46.       char *Drive;
  47.    } Arguments;
  48.  
  49. unsigned char __chip DMABuffer[512];
  50.  
  51. /***********************************************/
  52.  
  53. void CloseAll(int RC)
  54.  
  55. {
  56.    CloseDevice(&IORequest);
  57.    if (HardBlockMap) FreeMem(HardBlockMap,HardBlocksHi+1);
  58.    FreeArgs(RDArgs);
  59.    exit(RC);
  60. }
  61.  
  62. /***********************************************/
  63.  
  64. void LoadBlock(void *Buffer, int BufferSize, ULONG BlockNumber, ULONG BlockID)
  65.  
  66. {
  67.    int i;
  68.    ULONG SummedLongs;
  69.    LONG CheckSum;
  70.    ULONG *Temp;
  71.  
  72.    if (CheckSignal(SIGBREAKF_CTRL_C))
  73.       {
  74.          PrintFault(ERROR_BREAK,"InstallPassword");
  75.          CloseAll(RETURN_WARN);
  76.       }
  77.    IORequest.io_Command=CMD_READ;
  78.    IORequest.io_Length=TD_SECTOR;
  79.    IORequest.io_Data=(APTR)DMABuffer;
  80.    IORequest.io_Offset=BlockNumber*TD_SECTOR;
  81.    if (DoIO(&IORequest))
  82.       {
  83.          printf("Error reading block %ld\n",BlockNumber);
  84.          CloseAll(RETURN_ERROR);
  85.       }
  86.    if (Buffer)
  87.       {
  88.          __builtin_memcpy(Buffer,DMABuffer,BufferSize);
  89.          if (BlockID!=((struct RigidDiskBlock *)Buffer)->rdb_ID)
  90.             {
  91.                printf("Block %ld: exspected type 0x%lx got type 0x%lx\n",BlockNumber,BlockID,((struct RigidDiskBlock *)Buffer)->rdb_ID);
  92.                CloseAll(RETURN_ERROR);
  93.             }
  94.          SummedLongs=((struct RigidDiskBlock *)Buffer)->rdb_SummedLongs;
  95.          CheckSum=0;
  96.          Temp=(ULONG *)Buffer;
  97.          for (i=0; i<SummedLongs; i++)
  98.             {
  99.                CheckSum=CheckSum+(*Temp++);
  100.             }
  101.          if (CheckSum)
  102.             {
  103.                printf("Checksum of block %ld is invalid.\n",BlockNumber);
  104.                CloseAll(RETURN_ERROR);
  105.             }
  106.          if (HardBlockMap)
  107.             {
  108.                HardBlockMap[BlockNumber]=TRUE;
  109.             }
  110.       }
  111. }
  112.  
  113. /***********************************************/
  114.  
  115. void PrintLoadSegBlocks(ULONG Block)
  116.  
  117. {
  118.    while (Block!=NoBlock)
  119.       {
  120.          LoadBlock(&LoadSegBlock,sizeof(struct LoadSegBlock),Block,IDNAME_LOADSEG);
  121.          Block=LoadSegBlock.lsb_Next;
  122.       }
  123. }
  124.  
  125. /***********************************************/
  126.  
  127. void PrintFileSysHeaderBlock(struct FileSysHeaderBlock *FHB)
  128.  
  129. {
  130.    ULONG Block;
  131.  
  132.    if ((Block=FHB->fhb_SegListBlocks)==NoBlock) return;
  133.    PrintLoadSegBlocks(Block);
  134. }
  135.  
  136. /***********************************************/
  137.  
  138. void MakeHardblockMap(void)
  139.  
  140. {
  141.    ULONG Block;
  142.    int Count;
  143.  
  144.    Block=FALSE;
  145.    for (RDBBlock=0; RDBBlock<RDB_LOCATION_LIMIT && !Block; )
  146.       {
  147.          LoadBlock(NULL,512,RDBBlock,NULL);
  148.          if (*(ULONG *)DMABuffer==IDNAME_RIGIDDISK)
  149.             {
  150.                Block=TRUE;
  151.             }
  152.          else
  153.             {
  154.                RDBBlock++;
  155.             }
  156.       }
  157.    if (!Block)
  158.       {
  159.          printf("Sorry, I couldn't find the RigidDiskBlock on this unit!\n"
  160.                 "The password code could not be installed.\n");
  161.          CloseAll(RETURN_FAIL);
  162.       }
  163.    printf("Found the RigidDiskBlock on block %ld\n",RDBBlock);
  164.    LoadBlock(&RigidDiskBlock,sizeof(struct RigidDiskBlock),RDBBlock,IDNAME_RIGIDDISK);
  165.    if (RigidDiskBlock.rdb_DriveInit!=NoBlock)
  166.       {
  167.          printf("Sorry, I couldn't install the password code.\n"
  168.                 "Some DriveInit code is already installed on this unit!\n");
  169.          CloseAll(RETURN_ERROR);
  170.       }
  171.    HardBlocksHi=RigidDiskBlock.rdb_RDBBlocksHi;
  172.    if (HardBlockMap=AllocMem(HardBlocksHi+1,MEMF_CLEAR))
  173.       {
  174.          for (Block=0; Block<=RDBBlock; Block++)
  175.             {
  176.                HardBlockMap[Block]=TRUE;
  177.             }
  178.       }
  179.    else
  180.       {
  181.          printf("Out of memory.\n");
  182.          CloseAll(RETURN_ERROR);
  183.       }
  184.  
  185.    Block=RigidDiskBlock.rdb_BadBlockList;
  186.    if (Block!=NoBlock)
  187.       {
  188.          do
  189.             {
  190.                LoadBlock(&BadBlockBlock,sizeof(struct BadBlockBlock),Block,IDNAME_BADBLOCK);
  191.                Block=BadBlockBlock.bbb_Next;
  192.             }
  193.          while (Block!=NoBlock);
  194.       }
  195.  
  196.    Block=RigidDiskBlock.rdb_PartitionList;
  197.    if (Block!=NoBlock)
  198.       {
  199.          do
  200.             {
  201.                LoadBlock(&PartitionBlock,sizeof(struct PartitionBlock),Block,IDNAME_PARTITION);
  202.                Block=PartitionBlock.pb_Next;
  203.             }
  204.          while (Block!=NoBlock);
  205.       }
  206.  
  207.    Block=RigidDiskBlock.rdb_FileSysHeaderList;
  208.    if (Block!=NoBlock)
  209.       {
  210.          do
  211.             {
  212.                LoadBlock(&FileSysHeaderBlock,sizeof(struct FileSysHeaderBlock),Block,IDNAME_FILESYSHEADER);
  213.                PrintFileSysHeaderBlock(&FileSysHeaderBlock);
  214.                Block=FileSysHeaderBlock.fhb_Next;
  215.             }
  216.          while (Block!=NoBlock);
  217.       }
  218.  
  219.    Block=RigidDiskBlock.rdb_DriveInit;
  220.    if (Block!=NoBlock)
  221.       {
  222.          PrintLoadSegBlocks(Block);
  223.       }
  224.  
  225.    for (Block=0; Block<=HardBlocksHi; Block++)
  226.       {
  227.          if (!HardBlockMap[Block])
  228.             {
  229.                LoadBlock(NULL,512,Block,NULL);
  230.                if (*(ULONG *)DMABuffer==0x46494C4C)
  231.                   {
  232.                      HardBlockMap[Block]=TRUE;
  233.                   }
  234.             }
  235.       }
  236.  
  237.    Count=0;
  238.    for (Block=0; Block<=HardBlocksHi && Count<BLOCKS_NEEDED; Block++)
  239.       {
  240.          if (!HardBlockMap[Block])
  241.             {
  242.                Blocks[Count]=Block;
  243.                Count++;
  244.             }
  245.       }
  246.    if (Count!=BLOCKS_NEEDED)
  247.       {
  248.          printf("Sorry, I couldn't find a place to store the password code!\n");
  249.          CloseAll(RETURN_ERROR);
  250.       }
  251. }
  252.  
  253. /***********************************************/
  254.  
  255. void OpenDriveDevice(void)
  256.  
  257. {
  258.    struct DosList *DosList;
  259.    struct FileSysStartupMsg *StartupMsg;
  260.    char *Device;
  261.    ULONG Unit;
  262.    int RC;
  263.  
  264.    RC=0;
  265.    DosList=LockDosList(LDF_READ | LDF_DEVICES);
  266.    if (DosList=FindDosEntry(DosList,Arguments.Drive,LDF_DEVICES))
  267.       {
  268.          StartupMsg=(struct FileSysStartupMsg *)BADDR(DosList->dol_misc.dol_handler.dol_Startup);
  269.          IORequest.io_Message.mn_ReplyPort=&((struct Process *)SysBase->ThisTask)->pr_MsgPort;
  270.          Device=((char *)BADDR(StartupMsg->fssm_Device))+1;
  271.          Unit=StartupMsg->fssm_Unit;
  272.          if (OpenDevice(Device,Unit,&IORequest,0))
  273.             {
  274.                printf("Unable to open %s unit %lu\n",Device,Unit);
  275.                RC=RETURN_FAIL;
  276.             }
  277.          else
  278.             {
  279.                printf("Attempting to install password code on %s unit %ld\n",Device,Unit);
  280.             }
  281.       }
  282.    else
  283.       {
  284.          printf("\x22%s:\x22 not found.\n",Arguments.Drive);
  285.          RC=RETURN_FAIL;
  286.       }
  287.    UnLockDosList(LDF_READ | LDF_DEVICES);
  288.    if (RC) CloseAll(RC);
  289. }
  290.  
  291. /***********************************************/
  292.  
  293. void WriteDriveInitCode(void)
  294.  
  295. {
  296.    int Count, i;
  297.    ULONG Code;
  298.    long CheckSum;
  299.  
  300.    LoadBlock(&RigidDiskBlock,sizeof(struct RigidDiskBlock),RDBBlock,IDNAME_RIGIDDISK);
  301.    Code=(ULONG)DriveInitCode;
  302.    for (Count=0; Count<BLOCKS_NEEDED; Count++)
  303.       {
  304.          LoadSegBlock.lsb_ID=IDNAME_LOADSEG;
  305.          LoadSegBlock.lsb_ChkSum=0;
  306.          LoadSegBlock.lsb_HostID=RigidDiskBlock.rdb_HostID;
  307.          if (Count+1!=BLOCKS_NEEDED)
  308.             {
  309.                LoadSegBlock.lsb_Next=Blocks[Count+1];
  310.                LoadSegBlock.lsb_SummedLongs=TD_SECTOR>>2;
  311.                __builtin_memcpy(LoadSegBlock.lsb_LoadData,(char *)Code,123*sizeof(ULONG));
  312.                Code=Code+123*sizeof(ULONG);
  313.             }
  314.          else
  315.             {
  316.                LoadSegBlock.lsb_Next=NoBlock;
  317.                LoadSegBlock.lsb_SummedLongs=(((ULONG)CodeEnd-Code+3)>>2)+5*sizeof(ULONG);
  318.                __builtin_memcpy(LoadSegBlock.lsb_LoadData,(char *)Code,(ULONG)CodeEnd-Code);
  319.             }
  320.          CheckSum=0;
  321.          for (i=0; i<LoadSegBlock.lsb_SummedLongs; i++)
  322.             {
  323.                CheckSum+=((long *)&LoadSegBlock)[i];
  324.             }
  325.          LoadSegBlock.lsb_ChkSum=0-CheckSum;
  326.          printf("Writing block %ld\n",Blocks[Count]);
  327.          IORequest.io_Command=CMD_WRITE;
  328.          IORequest.io_Length=TD_SECTOR;
  329.          IORequest.io_Data=&LoadSegBlock;
  330.          IORequest.io_Offset=Blocks[Count]*TD_SECTOR;
  331.          if (DoIO(&IORequest))
  332.             {
  333.                printf("Error writing block %ld\n",Blocks[Count]);
  334.                CloseAll(RETURN_ERROR);
  335.             }
  336.       }
  337.    RigidDiskBlock.rdb_DriveInit=Blocks[0];
  338.    RigidDiskBlock.rdb_ChkSum=0;
  339.    CheckSum=0;
  340.    for (i=0; i<RigidDiskBlock.rdb_SummedLongs; i++)
  341.       {
  342.          CheckSum+=((long *)&RigidDiskBlock)[i];
  343.       }
  344.    RigidDiskBlock.rdb_ChkSum=0-CheckSum;
  345.    printf("Writing block %ld\n",RDBBlock);
  346.    IORequest.io_Command=CMD_WRITE;
  347.    IORequest.io_Length=TD_SECTOR;
  348.    IORequest.io_Data=&RigidDiskBlock;
  349.    IORequest.io_Offset=RDBBlock*TD_SECTOR;
  350.    if (DoIO(&IORequest))
  351.       {
  352.          printf("Error writing block %ld\n",RDBBlock);
  353.          CloseAll(RETURN_ERROR);
  354.       }
  355. }
  356.  
  357. /***********************************************/
  358.  
  359. void main(void)
  360.  
  361. {
  362.    int RC;
  363.  
  364.    RC=RETURN_FAIL;
  365.    if (CommandLineLength)
  366.       {
  367.          if (RDArgs=ReadArgs("DRIVE/A",(long *)&Arguments,NULL))
  368.             {
  369.                OpenDriveDevice();
  370.                MakeHardblockMap();
  371.                WriteDriveInitCode();
  372.                printf("Password V1.0 successfully installed.\n"
  373.                       "The current password is the empty password.\n"
  374.                       "Use the passwd command to change it.\n");
  375.                RC=RETURN_OK;
  376.             }
  377.          else
  378.             {
  379.                PrintFault(IoErr(),"InstallPassword");
  380.             }
  381.       }
  382.    CloseAll(RC);
  383. }
  384.